package cn.blt.core.article.service.impl;

import cn.blt.common.BaseEntity;
import cn.blt.core.article.entity.BlogTypeEntity;
import cn.blt.core.article.mapper.BlogTypeMapper;
import cn.blt.core.article.service.BlogTypeService;
import cn.blt.common.ResponseResult;
import cn.blt.common.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 博客类型
 * @author run
 * @date 2022/08/25
 */
@Service
@Slf4j
public class BlogTypeServiceImpl extends ServiceImpl<BlogTypeMapper, BlogTypeEntity> implements BlogTypeService {

    /**
     * 保存博客类型
     * @param typeEntity 类型实体
     * @return {@link Result}
     */
    @Override
    public Result saveType(BlogTypeEntity typeEntity) {
        try{
            log.info("saveType 入参 => {}",typeEntity);
            boolean flag = this.save(typeEntity);
            return ResponseResult.result(flag,"博客类型保存成功","博客类型保存失败");
        }catch(Exception e){
            log.error("saveType => {}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询博客类型
     * @param entity 实体
     * @return {@link Result}
     */
    @Override
    public Result queryBlogType(BaseEntity entity) {
        try{
            log.info("queryBlogType 入参 => {}",entity);
            PageInfo<BlogTypeEntity> page = PageHelper
                    .startPage(entity.getCurrent(), entity.getPageSize())
                    .doSelectPageInfo(this::list);
            return Result.ok(page);
        }catch(Exception e){
            log.error("queryBlogType => {}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
